home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8578 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.6 KB  |  228 lines

  1. Path: news0.accent.net!news
  2. From: Daniel Bolduc <eros@accent.net>
  3. Newsgroups: comp.lang.c
  4. Subject: Preprocessor problem
  5. Date: Mon, 04 Mar 1996 23:08:24 -0800
  6. Organization: EROS Inc.
  7. Message-ID: <313BE868.6AEA@accent.net>
  8. NNTP-Posting-Host: 205.236.55.156
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=iso-8859-1
  11. Content-Transfer-Encoding: 8bit
  12. X-Mailer: Mozilla 2.0 (Win16; I)
  13.  
  14. In the past, I programmed with the release  3.2.4 of SCO Operating and
  15. Development System.   The C compiler was Microsoft C 6.0.
  16.  
  17. With the new compiler  (norme AT&T) , i can use the maximum of the preprocessor without problem.
  18. The preprocessor must be recursive, he have to resolve expression until the code is reduce to the 
  19. simple code.  With  the last "cc" command, i never had any error.
  20.  
  21. But now, with the new compiler, it didn't resolve the expression recursively.
  22. It translate expression with the appropriate code, but if the code use a
  23. variable, the reference is not resolve and i get a compiler error.
  24.  
  25. I make an example of little source code to show the problem.  The problem is
  26. coming with the translate command  "?_FVAL( alias, field )".
  27. The expression        "S_FVAL( Order, iNumber )"
  28. generated this        "zaoFldOrder[ iNumberOrder ].u.cpValue"
  29.  
  30. The reference of "iNumberOrder" is known, but the compiler don't
  31. resolve it. But if i write the code correctly like a result of preprocess,
  32. the compiler don't give an error.
  33.  
  34. Does it have a setup to "cc" to eliminate this problem ?
  35.  
  36. If i compile the source with the parameters "-E -P" and i dump the output
  37. on other file.c and I compile again the second file, I resolve the problem.
  38.  
  39.  
  40. //========BEGIN OF TEST.C================================================
  41. // test.c
  42. // Daniel Bolduc, March 3, 1996
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46.  
  47. #define ON      1
  48. #define OFF     0
  49.  
  50. typedef int    logic;
  51. typedef int    bitmask;
  52. typedef char   string;
  53.  
  54. #define  function
  55. #define  local
  56.  
  57. #define min(a,b)  ( ( (a) < (b) ) ? (a) : (b) )
  58. #define max(a,b)  ( ( (a) > (b) ) ? (a) : (b) )
  59.  
  60. #define  Min   min
  61. #define  Max   max
  62.  
  63. #define  BoucleFor(_i_,_NbElem_)    for( _i_=0; _i_ < _NbElem_ ; _i_++ )
  64.  
  65. //=========================================================================
  66. // These defines produce the following result:
  67. //
  68. //    DATABASE_RECORD( Employee )
  69. //
  70. // preprocessed code is:
  71. //
  72. //    static int ziAreaEmployee = -1; static sFldEmployee [] = {
  73.  
  74. #define DATABASE_RECORD(idAlias) \
  75.    static int  ziArea##idAlias = -1;   \
  76.    static sFld zaoFld##idAlias [] = {
  77.  
  78. //=========================================================================
  79. // These defines produce the following result:
  80. //
  81. //    USE_FIELD( cName, STRING )
  82. //
  83. // preprocessed code is:
  84. //
  85. //    { "cName", STRING },
  86.  
  87. #define USE_FIELD(idFldName,idType) \
  88.    { #idFldName, idType },
  89.  
  90. #define END_OF_RECORD   \
  91.    { 0 } };
  92.  
  93. //=========================================================================
  94. // These defines produce the following result:
  95. //
  96. //    S_FVAL( Employee, cName )
  97. //
  98. // preprocess code is:
  99. //
  100. //      zaoFldEmployee [cNameEmployee] . u . cpValue
  101.  
  102. #define S_FVAL(idAlias,idFld) \
  103.    zaoFld##idAlias[##idFld##idAlias].u.cpValue
  104. #define C_FVAL(idAlias,idFld) \
  105.    zaoFld##idAlias[##idFld##idAlias].u.cValue
  106. #define I_FVAL(idAlias,idFld) \
  107.    zaoFld##idAlias[##idFld##idAlias].u.iValue
  108. #define L_FVAL(idAlias,idFld) \
  109.    zaoFld##idAlias[##idFld##idAlias].u.lValue
  110. #define R_FVAL(idAlias,idFld) \
  111.    zaoFld##idAlias[##idFld##idAlias].u.rValue
  112.  
  113. //=========================================================================
  114. #define  STRING   0
  115. #define  CHAR     1
  116. #define  INTEGER  2
  117. #define  LOGICAL  3
  118. #define  FLOAT    4
  119.  
  120. static string  *sacpType[] = { "STRING", "CHAR", "INTEGER", "LOGICAL", "FLOAT" };
  121.  
  122. //=========================================================================
  123. typedef struct
  124. {
  125.    string *cFieldName;
  126.    int    iType;
  127.    union
  128.    {
  129.       string   *cpValue;
  130.       char     cValue;
  131.       int      iValue;
  132.       logic    lValue;
  133.       float    rValue;
  134.    } u;
  135. } sFld;
  136.  
  137. //=========================================================================
  138.  
  139. DATABASE_RECORD( Test )
  140.    USE_FIELD( iNum,     INTEGER  )
  141.    USE_FIELD( cName,    STRING   )
  142.    USE_FIELD( cSex,     CHAR     )
  143.    USE_FIELD( lActive,  LOGICAL  )
  144.    USE_FIELD( rSalary,  FLOAT    )
  145. END_OF_RECORD
  146.  
  147. enum
  148. {
  149.    iNumTest, cNameTest, cSexTest, lActiveTest, rSalaryTest
  150. };
  151.  
  152. //=========================================================================
  153.  
  154. function main()
  155. {
  156.    local int   liX;
  157.    local sFld  *lopFld;
  158.  
  159.    I_FVAL( Test, iNum      )  = 2240;                // line 123
  160.    S_FVAL( Test, cName     )  = "Daniel Bolduc";        // line 124
  161.    C_FVAL( Test, cSex      )  = 'M';                // line 125
  162.    L_FVAL( Test, lActive   )  = ON;                // line 127
  163.    R_FVAL( Test, rSalary   )  = 19.75;                // line 128
  164.  
  165.    lopFld = zaoFldTest;
  166.    BoucleFor( liX, 5 )
  167.    {
  168.       printf( "%-20s %-20s Value='"
  169.                , lopFld->cFieldName
  170.                , sacpType [lopFld->iType]
  171.             );
  172.       switch( lopFld->iType )
  173.       {
  174.          case STRING:   printf( "%s'", lopFld->u.cpValue ); break;
  175.          case CHAR:     printf( "%c'", lopFld->u.cValue  ); break;
  176.          case INTEGER:  printf( "%d'", lopFld->u.iValue  ); break;
  177.          case LOGICAL:  printf( "%s'", lopFld->u.lValue ? "ON" : "OFF" ); break;
  178.          case FLOAT:    printf( "%f'", lopFld->u.rValue  ); break;
  179.       }
  180.       printf( "\n" );
  181.       ++lopFld;
  182.    }
  183. }
  184. //========END OF TEST.C================================================
  185.  
  186. After doing this command, i got these errors:
  187.  
  188. #cc test.c
  189.  
  190. "test.c", line 123: error: invalid token: [iNumTest
  191. "test.c", line 123: error: Syntax error before or at: ]
  192. "test.c", line 124: error: invalid token: [cNameTest
  193. "test.c", line 125: error: invalid token: [cSexTest
  194. "test.c", line 126: error: invalid token: [lActiveTest
  195. "test.c", line 127: error: invalid token: [rSalaryTest
  196.  
  197.  
  198. If i compile the source like this, the program works:
  199.  
  200. #cc -E -P test.c > test2.c
  201.  
  202. "test.c", line 123: error: invalid token: [iNumTest
  203. "test.c", line 123: error: Syntax error before or at: ]
  204. "test.c", line 124: error: invalid token: [cNameTest
  205. "test.c", line 125: error: invalid token: [cSexTest
  206. "test.c", line 126: error: invalid token: [lActiveTest
  207. "test.c", line 127: error: invalid token: [rSalaryTest
  208.  
  209. #cc test2.c
  210. #a.out
  211.  
  212. iNum                 INTEGER              Value='2240'
  213. cName                STRING               Value='Daniel Bolduc'
  214. cSex                 CHAR                 Value='M'
  215. lActive              LOGICAL              Value='ON'
  216. rSalary              FLOAT                Value='19.750000'
  217.  
  218. -- 
  219.   |\/\/\/|  
  220.   |      |  
  221.   |      |      Daniel Bolduc
  222.   | (o)(o)      eros@accent.net            or eros@total.net  (End of march)
  223.   C      _)     MontrΘal, QC, CANADA
  224.    | ,___|  
  225.    |   /    
  226.   /____\    
  227.  /      \
  228.